package com.tang.service.admin.remote.service;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.channel.ChannelExec;
import org.apache.sshd.client.channel.ChannelShell;
import org.apache.sshd.client.channel.ClientChannelEvent;
import org.apache.sshd.client.future.ConnectFuture;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.common.util.io.input.NoCloseInputStream;
import org.apache.sshd.common.util.io.output.NoCloseOutputStream;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.EnumSet;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

/**
 * @author tang jun liang
 * @Date 2023/6/15 8:09
 */
public class SshdTest {

    /**
     * 交互式命令发送
     *
     * @param conn
     * @param timeout
     * @throws IOException
     */
    public static ChannelShell runCommandForInteractive(SshConnection conn, long timeout)
            throws IOException {
        SshClient client = SshClient.setUpDefaultClient();

        try {
            // Open the client
            client.start();

            // Connect to the server
            ConnectFuture cf = client.connect(conn.getUsername(), conn.getHostname(), 22);
            ClientSession session = cf.verify().getSession();
            session.addPasswordIdentity(conn.getPassword());
            session.auth().verify(TimeUnit.SECONDS.toMillis(timeout));

            // Create the shell and channel its output/error streams
            ChannelShell cs = session.createShellChannel();
            cs.setOut(new NoCloseOutputStream(System.out));
            cs.setErr(new NoCloseOutputStream(System.err));
            cs.setIn(new NoCloseInputStream(System.in));

//    Execute and wait
            cs.open();
            cs.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), 0);
            cs.close(false);
            session.close(false);
            return cs;
        } finally {
            client.stop();
        }

    }

    public static void main(String[] args) throws IOException {
        // 创建用户信息
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        HttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();
        HttpGet httpGet = new HttpGet("https://harbor.coolteasocial.com:6443/Tag/v2.0/projects/cool-tea/repositories/socializing/artifacts?with_tag=true&with_label=false&with_scan_overview=false&with_signature=false&with_immutable_status=false&with_accessory=false");
        httpGet.setHeader("X-Accept-Vulnerabilities", "application/vnd.security.vulnerability.report; version=1.1, application/vnd.scanner.adapter.vuln.report.harbor+json; version=1.0");
        httpGet.setHeader(HttpHeaders.AUTHORIZATION, getBasicAuthHeaderValue("admin", "!@#Tang1234"));
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        System.out.println(EntityUtils.toString(entity));


    }

    private static String getBasicAuthHeaderValue(String username, String password) {
        String auth = username + ":" + password;
        byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
        return "Basic " + new String(encodedAuth);
    }
}
